﻿using Common.Http;
using Common.Msg;
using CommonData.Config;
using CToolsWin.DAL;
using CToolsWin.Model;
using HtmlAgilityPack;
using Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CToolsWin.BLL
{
    public class BLLITellYouResource : CToolsWin.IBLL.IGrab
    {
        #region 获取ITellYou类别信息
        private DALITellYouResource dal = new DALITellYouResource();

        /// <summary>
        /// 初始化表
        /// </summary>
        public void InitTable()
        {
            try
            {
                //初始化表
                Console.WriteLine("{0} ITellYou资源信息表初始化 - -", DateTime.Now);
                dal.InitTable();
                //获取商品分类信息
                Console.WriteLine("{0} ITellYou资源信息开始抓取 - -", DateTime.Now);
                Crawler();
            }
            catch (Exception ex)
            {
                Log.GetLog.Error("初始化数据出现错误", this.GetType(), ex);
            }
        }
        /// <summary>
        /// 爬虫
        /// </summary>
        public void Crawler()
        {
            try
            {
                DALITellYouCategory dalC = new DALITellYouCategory();
                List<ITellYouCategory> listC = dalC.QueryList(2);
                List<ITellYouResource> list = new List<ITellYouResource>();
                foreach (var C in listC)
                {
                    Console.WriteLine("开始获取{0}类", C.Name);
                    //获取语言版本
                    List<ResourceLanguage> listL = GetLanguageList(C.Code);
                    foreach (var l in listL)
                    {
                        Console.WriteLine("开始获取{0}版资源", l.lang);
                        //获取资源列表
                        list.AddRange(GetResourceList(C.ID, C.Code, l.id, l.lang));
                    }
                }
                dal.Save(list);
            }
            catch (Exception ex)
            {
                Log.GetLog.Error("获取内容出现错误", this.GetType(), ex);
            }
        }
        public static List<ResourceLanguage> GetLanguageList(string parent)
        {
            try
            {
                //获取html
                string html = HttpHelper.Post(HttpHelper.CreateHttpWebRequest(url: "http://www.itellyou.cn/Category/GetLang", strCookie: SystemConfig.ITellYouCookie,
                    accept: "*/*",
                    acceptEncoding: "gzip, deflate",
                    acceptLanguage: "zh-CN,zh;q=0.8",
                    connection: "keep-alive",
                    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                    xRequestedWith: "XMLHttpRequest",
                    host: "www.itellyou.cn",
                    origin: "http://www.itellyou.cn",
                    referer: "http://www.itellyou.cn/")
                    , Encoding.UTF8, new Dictionary<string, string>() { { "id", parent } });//读取数据

                int startPosition = html.IndexOf('[');
                html = html.Substring(startPosition, html.Length - 1 - startPosition);
                List<ResourceLanguage> list = Common.Serialize.JsonHelper.ToObject<List<ResourceLanguage>>(html);
                return list;
            }
            catch (Exception ex)
            {
                Log.GetLog.Error("获取资源语言版本信息错误", typeof(BLLITellYouResource), ex);
            }
            return null;
        }
        public static List<ITellYouResource> GetResourceList(long parentId, string parent, string langId, string langName, bool filter = true)
        {
            try
            {
                //获取html
                string html = HttpHelper.Post(HttpHelper.CreateHttpWebRequest(url: "http://www.itellyou.cn/Category/GetList", strCookie: SystemConfig.ITellYouCookie,
                    accept: "*/*",
                    acceptEncoding: "gzip, deflate",
                    acceptLanguage: "zh-CN,zh;q=0.8",
                    connection: "keep-alive",
                    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                    xRequestedWith: "XMLHttpRequest",
                    host: "www.itellyou.cn",
                    origin: "http://www.itellyou.cn",
                    referer: "http://www.itellyou.cn/")
                    , Encoding.UTF8, new Dictionary<string, string>() { { "id", parent }, { "lang", langId }, { "filter", filter.ToString() } });//读取数据

                int startPosition = html.IndexOf('[');
                html = html.Substring(startPosition, html.Length - 1 - startPosition);
                List<ResourceInfo> listI = Common.Serialize.JsonHelper.ToObject<List<ResourceInfo>>(html);

                List<ITellYouResource> list = new List<ITellYouResource>();
                foreach (var i in listI)
                {
                    ITellYouResource m = new ITellYouResource();
                    m.Code = i.id;
                    m.CreatedTime = DateTime.Now;
                    m.LangId = langId;
                    m.LangName = langName;
                    m.Name = i.name;
                    m.ParentCode = parent;
                    m.ParentId = parentId;
                    m.Post = i.post;
                    m.ResourceUrl = i.url;
                    list.Add(m);
                }
                return list;
            }
            catch (Exception ex)
            {
                Log.GetLog.Error("获取内容出现错误", typeof(BLLITellYouResource), ex);
            }
            return null;
        }
        #endregion
    }
    public class ResourceLanguage
    {
        public string id { get; set; }

        public string lang { get; set; }
    }
    public class ResourceInfo
    {
        public string id { get; set; }
        public string name { get; set; }
        public string post { get; set; }
        public string url { get; set; }
    }
}
